package filters

import (
	"boomlive.com/boomlive-common/configs"
	"github.com/beego/beego/v2/core/logs"
	beecontext "github.com/beego/beego/v2/server/web/context"
)

// 假设已知合法服务器名称的哈希值列表
func FilterServiceCode(ctx *beecontext.Context) {

	isOpen := configs.FilterServiceCodeIsOpen

	if isOpen == 0 {
		return
	}

	serviceCode := ctx.Input.Header("Boomlive-Service-Code")
	serviceKey := ctx.Input.Header("Boomlive-Service-Key")

	logs.Debug("FilterServiceCode-ServiceCode:", serviceCode)
	logs.Debug("FilterServiceCode-ServiceKey:", serviceKey)

	if serviceCode == "" || serviceKey == "" {
		requestValidateFail(ctx)
		return
	}

	// 校验 ServiceCode
	var realKey string
	if realKey = configs.ServiceKeys[serviceCode]; realKey == "" {
		logs.Debug("filterServiceCode validate failed!")
		requestValidateFail(ctx)
		return
	}

	if realKey != serviceKey {
		logs.Debug("filterServiceCode validate failed!")
		requestValidateFail(ctx)
		return
	}

	logs.Debug("filterServiceCode validate success!")
}
